//
//  EaseInfoTableViewCell.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/12/5.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "EaseInfoTableViewCell.h"

@implementation EaseInfoTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier model:(id<IMessageModel>)model{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier model:model];
    if (self) {
        self.recvBubbleBackgroundImage=[UIImage imageNamed:@"logistics_bj"];
        self.selectionStyle=0;
    }
    return self;
}
+(NSString *)cellIdentifierWithModel:(id<IMessageModel>)model{
    return @"EaseInfoTableViewCell";
}
-(BOOL) isCustomBubbleView:(id)model{
    return YES;
}
-(void)setCustomModel:(id<IMessageModel>)model{
    UIImage *image = model.image;
    if (!image) {
        [self.bubbleView.imageView sd_setImageWithURL:[NSURL URLWithString:model.fileURLPath] placeholderImage:[UIImage imageNamed:model.failImageName]];
    } else {
        _bubbleView.imageView.image = image;
    }
    
    if (model.avatarURLPath) {
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatarURLPath] placeholderImage:model.avatarImage];
    } else {
        self.avatarView.image = model.avatarImage;
    }
}

-(void)setCustomBubbleView:(id<IMessageModel>)model{
    self.avatarView.hidden=YES;
    _bubbleView.imageView.image = [UIImage imageNamed:@"imageDownloadFail"];
    NSString *text=model.message.ext[@"title"];
    UILabel *label=[[UILabel alloc]init];
    CGSize testSize=[text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    label.frame=CGRectMake(15, 10, testSize.width, testSize.height);
    label.numberOfLines=0;
    label.text=text;
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=Red_Color;
    [self.bubbleView insertSubview:label aboveSubview:self.bubbleView.backgroundImageView];
    UILabel *logLabel=[[UILabel alloc]init];
    logLabel.text=[NSString stringWithFormat:@"%@",model.message.ext[@"content"]];
    //CGSize logSize=[logLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
    CGFloat height=[self getHeightByWidth:MainW-50 title:logLabel.text font:FontAndStyle12];
    logLabel.frame=CGRectMake(15, label.bsg_bottom+10, MainW-50, height);
    //logLabel.bsg_bottom=label.bsg_bottom;
    logLabel.numberOfLines=0;
    logLabel.font=[UIFont systemFontOfSize:12];
    logLabel.textColor=TextColor6;
    [self.bubbleView insertSubview:logLabel aboveSubview:self.bubbleView.backgroundImageView];
    
    
}
-(void)updateCustomBubbleViewMargin:(UIEdgeInsets)bubbleMargin model:(id<IMessageModel>)model{
    NSString *text=model.message.ext[@"content"];
    CGFloat height=[self getHeightByWidth:MainW-50 title:text font:FontAndStyle12];
    _bubbleView.translatesAutoresizingMaskIntoConstraints=YES;
    if (!model.isSender) {
        _bubbleView.frame=CGRectMake(10, 20, MainW-20, 45+height);
    }
}
- (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}

@end
